﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ReadyApps.Common.Exceptions;
using ReadyApps.Data;
using ReadyApps.Wiki;
using ReadyApps.Wiki.Views;
using ReadyAppsTest.Data;
using ReadyAppsTest.Common;
using ReadyApps.Common;
using ReadyApps.Data.Wiki;
using ReadyApps.Wiki.ViewModels;

namespace ReadyAppsTest.Wiki
{
    /// <summary>
    /// Summary description for WikiControllerTest
    /// </summary>
    [TestClass]
    public class WikiControllerTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Constructor Tests

        [TestMethod]
        public void WikiControllerConstructorTest()
        {
            TestWikiPageRequest pageRequest = new TestWikiPageRequest();
            pageRequest.CurrentUsername = "user";
            TestWikiPageState pageState = new TestWikiPageState();
            WikiControlSettings controlSettings = new WikiControlSettings();
            TestWikiDataProvider dataProvider = new TestWikiDataProvider();
            TestWikiUrlManager urlManager = new TestWikiUrlManager();
            TestWikiRoleManager roleManager = new TestWikiRoleManager();
            ControlCollection controls = new ControlCollection(new Page());
            WikiController target = new WikiController(pageRequest, pageState, controlSettings, dataProvider, urlManager, roleManager, controls);
            PrivateObject targetWrapper = new PrivateObject(target);
            Assert.AreEqual(pageRequest, targetWrapper.GetField("pageRequest"));
            Assert.AreEqual(pageState, targetWrapper.GetField("pageState"));
            Assert.AreEqual(controlSettings, targetWrapper.GetField("controlSettings"));
            Assert.AreEqual(dataProvider, targetWrapper.GetField("dataProvider"));
            Assert.AreEqual(urlManager, targetWrapper.GetField("urlManager"));
            Assert.AreEqual(controls, targetWrapper.GetField("controls"));
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void WikiControllerPageRequestNullTest()
        {
            TestWikiPageRequest pageRequest = null;
            TestWikiPageState pageState = new TestWikiPageState();
            WikiControlSettings controlSettings = new WikiControlSettings();
            TestWikiDataProvider dataProvider = new TestWikiDataProvider();
            TestWikiUrlManager urlManager = new TestWikiUrlManager();
            TestWikiRoleManager roleManager = new TestWikiRoleManager();
            ControlCollection controls = new ControlCollection(new Page());
            WikiController target = new WikiController(pageRequest, pageState, controlSettings, dataProvider, urlManager, roleManager, controls);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void WikiControllerPageStateNullTest()
        {
            TestWikiPageRequest pageRequest = new TestWikiPageRequest();
            TestWikiPageState pageState = null;
            WikiControlSettings controlSettings = new WikiControlSettings();
            TestWikiDataProvider dataProvider = new TestWikiDataProvider();
            TestWikiUrlManager urlManager = new TestWikiUrlManager();
            TestWikiRoleManager roleManager = new TestWikiRoleManager();
            ControlCollection controls = new ControlCollection(new Page());
            WikiController target = new WikiController(pageRequest, pageState, controlSettings, dataProvider, urlManager, roleManager, controls);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void WikiControllerControlSettingsNullTest()
        {
            TestWikiPageRequest pageRequest = new TestWikiPageRequest();
            TestWikiPageState pageState = new TestWikiPageState();
            WikiControlSettings controlSettings = null;
            TestWikiDataProvider dataProvider = new TestWikiDataProvider();
            TestWikiUrlManager urlManager = new TestWikiUrlManager();
            TestWikiRoleManager roleManager = new TestWikiRoleManager();
            ControlCollection controls = new ControlCollection(new Page());
            WikiController target = new WikiController(pageRequest, pageState, controlSettings, dataProvider, urlManager, roleManager, controls);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void WikiControllerDataProviderNullTest()
        {
            TestWikiPageRequest pageRequest = new TestWikiPageRequest();
            TestWikiPageState pageState = new TestWikiPageState();
            WikiControlSettings controlSettings = new WikiControlSettings();
            TestWikiDataProvider dataProvider = null;
            TestWikiUrlManager urlManager = new TestWikiUrlManager();
            TestWikiRoleManager roleManager = new TestWikiRoleManager();
            ControlCollection controls = new ControlCollection(new Page());
            WikiController target = new WikiController(pageRequest, pageState, controlSettings, dataProvider, urlManager, roleManager, controls);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void WikiControllerUrlManagerNullTest()
        {
            TestWikiPageRequest pageRequest = new TestWikiPageRequest();
            TestWikiPageState pageState = new TestWikiPageState();
            WikiControlSettings controlSettings = new WikiControlSettings();
            TestWikiDataProvider dataProvider = new TestWikiDataProvider();
            TestWikiUrlManager urlManager = null;
            TestWikiRoleManager roleManager = new TestWikiRoleManager();
            ControlCollection controls = new ControlCollection(new Page());
            WikiController target = new WikiController(pageRequest, pageState, controlSettings, dataProvider, urlManager, roleManager, controls);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void WikiControllerRoleManagerNullTest()
        {
            TestWikiPageRequest pageRequest = new TestWikiPageRequest();
            TestWikiPageState pageState = new TestWikiPageState();
            WikiControlSettings controlSettings = new WikiControlSettings();
            TestWikiDataProvider dataProvider = new TestWikiDataProvider();
            TestWikiUrlManager urlManager = new TestWikiUrlManager();
            TestWikiRoleManager roleManager = null;
            ControlCollection controls = new ControlCollection(new Page());
            WikiController target = new WikiController(pageRequest, pageState, controlSettings, dataProvider, urlManager, roleManager, controls);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void WikiControllerControlsNullTest()
        {
            TestWikiPageRequest pageRequest = new TestWikiPageRequest();
            TestWikiPageState pageState = new TestWikiPageState();
            WikiControlSettings controlSettings = new WikiControlSettings();
            TestWikiDataProvider dataProvider = new TestWikiDataProvider();
            TestWikiUrlManager urlManager = new TestWikiUrlManager();
            TestWikiRoleManager roleManager = new TestWikiRoleManager();
            ControlCollection controls = null;
            WikiController target = new WikiController(pageRequest, pageState, controlSettings, dataProvider, urlManager, roleManager, controls);
        }

        #endregion

        #region EnsurePageID Tests

        [TestMethod]
        public void EnsurePageIDInitializedPageFoundTest()
        {
            TestWikiPageRequest pageRequest = new TestWikiPageRequest();
            pageRequest.CurrentUsername = "user";
            pageRequest.SiteName = "SiteName";
            pageRequest.FullPath = "/Full/Path/";
            pageRequest.RequestedPage = "Page";
            TestWikiPageState pageState = new TestWikiPageState();
            WikiControlSettings controlSettings = new WikiControlSettings();
            TestWikiDataProvider dataProvider = new TestWikiDataProvider();
            TestWikiUrlManager urlManager = new TestWikiUrlManager();
            TestWikiRoleManager roleManager = new TestWikiRoleManager();
            ControlCollection controls = new ControlCollection(new Page());
            WikiController target = new WikiController(pageRequest, pageState, controlSettings, dataProvider, urlManager, roleManager, controls);
            PrivateObject targetWrapper = new PrivateObject(target);

            pageState.PageID = 1;
            pageState.IsInitialized = true;
            pageState.PageFound = true;
            PageDetails details = new PageDetails(false, false, false, true, string.Empty);
            dataProvider.LookupPage_ReturnValue = new PageLookup(2, details);
            dataProvider.GetPageDetails_ReturnValue = details;
            targetWrapper.Invoke("EnsurePageID");

            Assert.IsNull(dataProvider.LookupPage_siteName);
            Assert.IsNull(dataProvider.LookupPage_folder);
            Assert.IsNull(dataProvider.LookupPage_pageName);
            Assert.AreEqual(pageState.PageID, dataProvider.GetPageDetails_pageID);
            Assert.AreEqual((PageDetails)targetWrapper.GetField("pageDetails"), details);
        }

        [TestMethod]
        public void EnsurePageIDInitializedPageNotFoundTest()
        {
            TestWikiPageRequest pageRequest = new TestWikiPageRequest();
            pageRequest.CurrentUsername = "user";
            pageRequest.SiteName = "SiteName";
            pageRequest.FullPath = "/Full/Path/";
            pageRequest.RequestedPage = "Page";
            TestWikiPageState pageState = new TestWikiPageState();
            WikiControlSettings controlSettings = new WikiControlSettings();
            TestWikiDataProvider dataProvider = new TestWikiDataProvider();
            TestWikiUrlManager urlManager = new TestWikiUrlManager();
            TestWikiRoleManager roleManager = new TestWikiRoleManager();
            ControlCollection controls = new ControlCollection(new Page());
            WikiController target = new WikiController(pageRequest, pageState, controlSettings, dataProvider, urlManager, roleManager, controls);
            PrivateObject targetWrapper = new PrivateObject(target);

            pageState.PageID = 1;
            pageState.IsInitialized = true;
            pageState.PageFound = false;
            PageDetails details = new PageDetails(false, false, false, true, string.Empty);
            dataProvider.LookupPage_ReturnValue = new PageLookup(2, details);
            dataProvider.GetPageDetails_ReturnValue = details;
            targetWrapper.Invoke("EnsurePageID");

            Assert.IsNull(dataProvider.LookupPage_siteName);
            Assert.IsNull(dataProvider.LookupPage_folder);
            Assert.IsNull(dataProvider.LookupPage_pageName);
            Assert.AreEqual(0, dataProvider.GetPageDetails_pageID);
            Assert.IsNull((PageDetails)targetWrapper.GetField("pageDetails"));
        }

        [TestMethod]
        public void EnsurePageIDLookupPageNotFoundTest()
        {
            TestWikiPageRequest pageRequest = new TestWikiPageRequest();
            pageRequest.CurrentUsername = "user";
            pageRequest.SiteName = "siteName";
            pageRequest.RequestedPage = "pageName";
            pageRequest.FullPath = "full/path";
            TestWikiPageState pageState = new TestWikiPageState();
            WikiControlSettings controlSettings = new WikiControlSettings();
            TestWikiDataProvider dataProvider = new TestWikiDataProvider();
            TestWikiUrlManager urlManager = new TestWikiUrlManager();
            TestWikiRoleManager roleManager = new TestWikiRoleManager();
            ControlCollection controls = new ControlCollection(new Page());
            WikiController target = new WikiController(pageRequest, pageState, controlSettings, dataProvider, urlManager, roleManager, controls);
            PrivateObject targetWrapper = new PrivateObject(target);

            pageState.PageID = 1;
            pageState.IsInitialized = false;
            dataProvider.LookupPage_ReturnValue = PageLookup.PageNotFoundLookup;
            int pageCallCount = pageState.InitializePageNotFound_CallCount;
            int siteCallCount = pageState.InitializeSiteNotFound_CallCount;
            targetWrapper.Invoke("EnsurePageID");

            Assert.AreEqual(pageCallCount + 1, pageState.InitializePageNotFound_CallCount);
            Assert.AreEqual(siteCallCount, pageState.InitializeSiteNotFound_CallCount);
            Assert.AreEqual(pageRequest.SiteName, dataProvider.LookupPage_siteName);
            Assert.AreEqual(pageRequest.RequestedPage, dataProvider.LookupPage_pageName);
            Assert.AreEqual(pageRequest.FullPath, dataProvider.LookupPage_folder);
            Assert.AreEqual(0, dataProvider.GetPageDetails_pageID);
        }

        [TestMethod]
        public void EnsurePageIDLookupSiteNotFoundTest()
        {
            TestWikiPageRequest pageRequest = new TestWikiPageRequest();
            pageRequest.CurrentUsername = "user";
            pageRequest.SiteName = "siteName";
            pageRequest.RequestedPage = "pageName";
            pageRequest.FullPath = "full/path";
            TestWikiPageState pageState = new TestWikiPageState();
            WikiControlSettings controlSettings = new WikiControlSettings();
            TestWikiDataProvider dataProvider = new TestWikiDataProvider();
            TestWikiUrlManager urlManager = new TestWikiUrlManager();
            TestWikiRoleManager roleManager = new TestWikiRoleManager();
            ControlCollection controls = new ControlCollection(new Page());
            WikiController target = new WikiController(pageRequest, pageState, controlSettings, dataProvider, urlManager, roleManager, controls);
            PrivateObject targetWrapper = new PrivateObject(target);

            pageState.PageID = 1;
            pageState.IsInitialized = false;
            dataProvider.LookupPage_ReturnValue = PageLookup.SiteNotFoundLookup;
            int pageCallCount = pageState.InitializePageNotFound_CallCount;
            int siteCallCount = pageState.InitializeSiteNotFound_CallCount;
            targetWrapper.Invoke("EnsurePageID");

            Assert.AreEqual(pageCallCount, pageState.InitializePageNotFound_CallCount);
            Assert.AreEqual(siteCallCount + 1, pageState.InitializeSiteNotFound_CallCount);
            Assert.AreEqual(pageRequest.SiteName, dataProvider.LookupPage_siteName);
            Assert.AreEqual(pageRequest.RequestedPage, dataProvider.LookupPage_pageName);
            Assert.AreEqual(pageRequest.FullPath, dataProvider.LookupPage_folder);
            Assert.AreEqual(0, dataProvider.GetPageDetails_pageID);
        }

        [TestMethod]
        public void EnsurePageIDLookupPageFoundTest()
        {
            TestWikiPageRequest pageRequest = new TestWikiPageRequest();
            pageRequest.CurrentUsername = "user";
            pageRequest.SiteName = "siteName";
            pageRequest.RequestedPage = "pageName";
            pageRequest.FullPath = "full/path";
            TestWikiPageState pageState = new TestWikiPageState();
            WikiControlSettings controlSettings = new WikiControlSettings();
            TestWikiDataProvider dataProvider = new TestWikiDataProvider();
            TestWikiUrlManager urlManager = new TestWikiUrlManager();
            TestWikiRoleManager roleManager = new TestWikiRoleManager();
            ControlCollection controls = new ControlCollection(new Page());
            WikiController target = new WikiController(pageRequest, pageState, controlSettings, dataProvider, urlManager, roleManager, controls);
            PrivateObject targetWrapper = new PrivateObject(target);

            PageDetails details = PageDetails.Empty;
            dataProvider.LookupPage_ReturnValue = new PageLookup(11, details);
            targetWrapper.Invoke("EnsurePageID");

            Assert.AreEqual(pageState.Initialize_pageID, dataProvider.LookupPage_ReturnValue.PageID);
            Assert.AreEqual(details, (PageDetails)targetWrapper.GetField("pageDetails"));
            Assert.AreEqual(pageRequest.SiteName, dataProvider.LookupPage_siteName);
            Assert.AreEqual(pageRequest.RequestedPage, dataProvider.LookupPage_pageName);
            Assert.AreEqual(pageRequest.FullPath, dataProvider.LookupPage_folder);
            Assert.AreEqual(0, dataProvider.GetPageDetails_pageID);
        }

        #endregion

        #region GetView Tests

        [TestMethod]
        public void GetViewValidOptionsTest()
        {
            TestWikiPageRequest pageRequest = new TestWikiPageRequest();
            pageRequest.CurrentUsername = "user";
            pageRequest.BaseFolder = "/";
            pageRequest.SiteName = "SiteName";
            pageRequest.FullPath = "/Full/Path/";
            pageRequest.RequestedPage = "Page";
            TestWikiPageState pageState = new TestWikiPageState();
            pageState.PageID = 2;
            pageState.IsInitialized = true;
            pageState.RevisionNumber = 1;
            WikiControlSettings controlSettings = new WikiControlSettings();
            TestWikiDataProvider dataProvider = new TestWikiDataProvider();
            TestWikiUrlManager urlManager = new TestWikiUrlManager();
            TestWikiRoleManager roleManager = new TestWikiRoleManager();
            ControlCollection controls = new ControlCollection(new Page());

            WikiController target = new WikiController(pageRequest, pageState, controlSettings, dataProvider,
                urlManager, roleManager, controls);
            PrivateObject targetWrapper = new PrivateObject(target);

            // Inject the test security manager
            TestWikiSecurityManager securityManager = new TestWikiSecurityManager();
            targetWrapper.SetField("securityManager", securityManager);
            targetWrapper.SetField("pageDetails", PageDetails.Empty);

            // PageFound, SiteFound, Mode, Authorized, Result
            List<Tuple<bool, bool, bool, WikiPageMode, Type>> testCases = new List<Tuple<bool, bool, bool, WikiPageMode, Type>>();
            testCases.Add(new Tuple<bool, bool, bool, WikiPageMode, Type>(true, true, true, WikiPageMode.ViewPage, typeof(NormalView)));
            testCases.Add(new Tuple<bool, bool, bool, WikiPageMode, Type>(true, true, true, WikiPageMode.EditPage, typeof(EditView)));
            testCases.Add(new Tuple<bool, bool, bool, WikiPageMode, Type>(true, true, true, WikiPageMode.CreatePage, typeof(NewPageView)));
            testCases.Add(new Tuple<bool, bool, bool, WikiPageMode, Type>(true, true, true, WikiPageMode.EditPermissions, typeof(EditPermissionsView)));
            testCases.Add(new Tuple<bool, bool, bool, WikiPageMode, Type>(true, true, true, WikiPageMode.ViewRevisionList, typeof(RevisionListView)));
            testCases.Add(new Tuple<bool, bool, bool, WikiPageMode, Type>(true, true, true, WikiPageMode.ViewPageRevision, typeof(RevisionView)));
            testCases.Add(new Tuple<bool, bool, bool, WikiPageMode, Type>(true, true, true, WikiPageMode.ViewPagePending, typeof(PendingView)));
            testCases.Add(new Tuple<bool, bool, bool, WikiPageMode, Type>(true, true, true, WikiPageMode.PageSettings, typeof(PageSettingsView)));
            testCases.Add(new Tuple<bool, bool, bool, WikiPageMode, Type>(true, true, false, WikiPageMode.ViewPage, typeof(NotAuthorizedView)));
            testCases.Add(new Tuple<bool, bool, bool, WikiPageMode, Type>(false, false, true, WikiPageMode.ViewPage, typeof(NewSiteView)));
            testCases.Add(new Tuple<bool, bool, bool, WikiPageMode, Type>(false, false, false, WikiPageMode.ViewPage, typeof(NewSiteView)));
            testCases.Add(new Tuple<bool, bool, bool, WikiPageMode, Type>(false, true, true, WikiPageMode.ViewPage, typeof(NotFoundView)));
            testCases.Add(new Tuple<bool, bool, bool, WikiPageMode, Type>(false, true, false, WikiPageMode.ViewPage, typeof(NotFoundView)));

            foreach (var testCase in testCases)
            {
                pageState.PageFound = testCase.Item1;
                pageState.SiteFound = testCase.Item2;
                securityManager.WikiPageModeAuthorized = testCase.Item3;
                pageState.PageMode = testCase.Item4;
                try
                {
                    IWikiView actual = (IWikiView)targetWrapper.Invoke("GetView");
                    Assert.IsInstanceOfType(actual, testCase.Item5, string.Format(
                        "Failed on PageFound = {0}, SiteFound = {1}, Authorized = {2}, expected = '{3}', got '{4}'",
                        testCase.Item1, testCase.Item2, testCase.Item3, testCase.Item4, actual.GetType()));
                }
                catch (Exception ex)
                {
                    Assert.Fail(string.Format(
                        "Failed on PageFound = {0}, SiteFound = {1}, Authorized = {2}, expected = '{3}', Exception '{4}'",
                        testCase.Item1, testCase.Item2, testCase.Item3, testCase.Item4, ex));
                }
            }
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidEnumerationValueException))]
        public void GetViewInvalidModeTest()
        {
            TestWikiPageRequest pageRequest = new TestWikiPageRequest();
            pageRequest.CurrentUsername = "user";
            TestWikiPageState pageState = new TestWikiPageState();
            pageState.IsInitialized = true;
            pageState.PageID = 1;
            pageState.PageFound = true;
            pageState.PageMode = (WikiPageMode)1000;
            WikiControlSettings controlSettings = new WikiControlSettings();
            TestWikiDataProvider dataProvider = new TestWikiDataProvider();
            TestWikiUrlManager urlManager = new TestWikiUrlManager();
            TestWikiRoleManager roleManager = new TestWikiRoleManager();
            ControlCollection controls = new ControlCollection(new Page());

            WikiController target = new WikiController(pageRequest, pageState, controlSettings, dataProvider,
                urlManager, roleManager, controls);
            PrivateObject targetWrapper = new PrivateObject(target);

            // Inject the test security manager
            TestWikiSecurityManager securityManager = new TestWikiSecurityManager();
            targetWrapper.SetField("securityManager", securityManager);
            securityManager.WikiPageModeAuthorized = true;

            targetWrapper.SetField("pageDetails", PageDetails.Empty);

            targetWrapper.Invoke("GetView");
        }

        #endregion

        #region SelectPageMode Tests

        [TestMethod]
        public void SelectPageModeValidTest()
        {
            PageDetails details = new PageDetails(false, false, false, false, string.Empty);
            TestWikiSecurityManager securityManager = new TestWikiSecurityManager();
            WikiPageMode actual;
            PrivateType target = new PrivateType(typeof(WikiController));

            // Publishable, Pending, Username, SameUser, Result
            List<Tuple<bool, bool, string, bool, WikiPageMode>> testCases = new List<Tuple<bool, bool, string, bool, WikiPageMode>>();
            testCases.Add(new Tuple<bool, bool, string, bool, WikiPageMode>(false, false, string.Empty, false, WikiPageMode.ViewPage));
            testCases.Add(new Tuple<bool, bool, string, bool, WikiPageMode>(false, true, string.Empty, false, WikiPageMode.ViewPage));
            testCases.Add(new Tuple<bool, bool, string, bool, WikiPageMode>(true, false, string.Empty, false, WikiPageMode.ViewPage));
            testCases.Add(new Tuple<bool, bool, string, bool, WikiPageMode>(true, true, string.Empty, false, WikiPageMode.ViewPage));
            testCases.Add(new Tuple<bool, bool, string, bool, WikiPageMode>(true, true, string.Empty, true, WikiPageMode.ViewPagePending));

            foreach (var testCase in testCases)
            {
                details.Publishable = testCase.Item1;
                details.PendingRevision = testCase.Item2;
                details.PendingRevisionAuthor = testCase.Item3;
                securityManager.IsSameUser_ReturnValue = testCase.Item4;
                actual = (WikiPageMode)target.InvokeStatic("SelectPageMode", securityManager, details);
                Assert.AreEqual(actual, testCase.Item5, string.Format(
                    "Failed on Publishable = {0}, Pending = {1}, Username = {2}, SameUser = {3}",
                    testCase.Item1, testCase.Item2, testCase.Item3, testCase.Item4));
            }
        }

        #endregion

        #region GetPageContentFromRawText Tests

        [TestMethod]
        public void MyTestMethod()
        {
            string title = "Title";
            string rawText = "<p><a href=\"javascript:void(0);\" pageid=\"1\">IT Notebook Index</a> &rarr; <a href=\"javascript:void(0);\" pageid=\"2\">Oracle Articles</a></p>";
            string processedText = "<p>{[link:1]} &rarr; {[link:2]}</p>";
            PageContent expected = new PageContent(title, processedText,
                new[] {
                    new InternalPageLink(1, 1, String.Empty, "IT Notebook Index", String.Empty),
                    new InternalPageLink(2, 2, String.Empty, "Oracle Articles", String.Empty)
                });
            PrivateType target = new PrivateType(typeof(WikiController));
            PageContent actual;

            actual = (PageContent)target.InvokeStatic("GetPageContentFromRawText", title, rawText);
            Assert.AreEqual(expected, actual);
        }

        #endregion

        #region Event Handler Tests

        [TestMethod]
        public void PageSettings_Save_ClickPublishingTest()
        {
            TestWikiPageRequest pageRequest = new TestWikiPageRequest();
            pageRequest.CurrentUsername = "user";
            TestWikiPageState pageState = new TestWikiPageState();
            WikiControlSettings controlSettings = new WikiControlSettings();
            TestWikiDataProvider dataProvider = new TestWikiDataProvider();
            TestWikiUrlManager urlManager = new TestWikiUrlManager();
            TestWikiRoleManager roleManager = new TestWikiRoleManager();
            ControlCollection controls = new ControlCollection(new Page());
            WikiController target = new WikiController(pageRequest, pageState, controlSettings, dataProvider, urlManager, roleManager, controls);
            PrivateObject targetWrapper = new PrivateObject(target);

            // Inject page details
            PageDetails details = PageDetails.Empty;
            targetWrapper.SetField("pageDetails", details);

            // CanManagePublishing, Publishable, PageID, DP PageID, DP Publishable
            List<Tuple<bool, bool, int, int, bool>> testCases = new List<Tuple<bool, bool, int, int, bool>>();
            testCases.Add(new Tuple<bool, bool, int, int, bool>(false, false, 1, 0, false));
            testCases.Add(new Tuple<bool, bool, int, int, bool>(true, false, 2, 2, false));
            testCases.Add(new Tuple<bool, bool, int, int, bool>(true, true, 3, 3, true));

            foreach (var testCase in testCases)
            {
                SaveViewModelEventArgs args = new SaveViewModelEventArgs(
                    new PageSettingsViewModel(testCase.Item1, false, new PublicationSettings(testCase.Item2), null));
                pageState.PageID = testCase.Item3;

                // Set the items to default values
                pageState.ChangePageMode(WikiPageMode.Unknown);
                details.Publishable = !testCase.Item2; // Make sure we set it to the opposite of the result
                dataProvider.SavePublicationSettings(0, null);

                targetWrapper.Invoke("PageSettings_Save_Click", null, args);

                Assert.AreEqual(dataProvider.SavePublicationSettings_pageID, testCase.Item4);
                Assert.AreEqual(pageState.ChangePageMode_newMode, WikiPageMode.ViewPage);
                if (testCase.Item4 != 0)
                {
                    Assert.IsNotNull(dataProvider.SavePublicationSettings_publicationSettings);
                    Assert.AreEqual(testCase.Item5, dataProvider.SavePublicationSettings_publicationSettings.Publishable);
                }
                if (testCase.Item1)
                {
                    Assert.AreEqual(testCase.Item5, details.Publishable);
                }
            }
        }

        [TestMethod]
        public void PageSettings_Save_ClickRevisionTest()
        {
            TestWikiPageRequest pageRequest = new TestWikiPageRequest();
            pageRequest.CurrentUsername = "user";
            TestWikiPageState pageState = new TestWikiPageState();
            WikiControlSettings controlSettings = new WikiControlSettings();
            TestWikiDataProvider dataProvider = new TestWikiDataProvider();
            TestWikiUrlManager urlManager = new TestWikiUrlManager();
            TestWikiRoleManager roleManager = new TestWikiRoleManager();
            ControlCollection controls = new ControlCollection(new Page());
            WikiController target = new WikiController(pageRequest, pageState, controlSettings, dataProvider, urlManager, roleManager, controls);
            PrivateObject targetWrapper = new PrivateObject(target);

            // Inject page details
            PageDetails details = PageDetails.Empty;
            targetWrapper.SetField("pageDetails", details);

            // CanManageVersioning, Versioned, Settings, DP Settings, PS Versioned
            List<Tuple<bool, bool, RetentionSettings, bool>> testCases = new List<Tuple<bool, bool, RetentionSettings, bool>>();
            testCases.Add(new Tuple<bool, bool, RetentionSettings, bool>(false, true, null, true));
            testCases.Add(new Tuple<bool, bool, RetentionSettings, bool>(true, true, new RetentionSettings(1, RetentionUnit.None, 0), false));
            testCases.Add(new Tuple<bool, bool, RetentionSettings, bool>(true, false, new RetentionSettings(2, RetentionUnit.Versions, 2), true));
            testCases.Add(new Tuple<bool, bool, RetentionSettings, bool>(true, true, new RetentionSettings(3, RetentionUnit.None, 0), false));

            foreach (var testCase in testCases)
            {
                SaveViewModelEventArgs args = new SaveViewModelEventArgs(
                    new PageSettingsViewModel(false, testCase.Item1, null, testCase.Item3));
                details.Versioned = testCase.Item2;

                // Set the items to default values
                pageState.ChangePageMode(WikiPageMode.Unknown);
                dataProvider.SaveRetentionSettings(null);

                targetWrapper.Invoke("PageSettings_Save_Click", null, args);

                Assert.AreEqual(pageState.ChangePageMode_newMode, WikiPageMode.ViewPage);
                Assert.AreEqual(testCase.Item4, details.Versioned);
                Assert.AreEqual(testCase.Item3, dataProvider.SaveRetentionSettings_retentionSettings);
            }
        }

        #endregion
    }
}
